package com.lmqb.controller;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lmqb.bean.LmLoan;
import com.lmqb.bean.LmRepay;
import com.lmqb.bean.SysConfig;
import com.lmqb.entry.MenuTitle;
import com.lmqb.entry.UBaseInfo;
import com.lmqb.service.CfgParamsService;
import com.lmqb.service.RepayService;
import com.lmqb.service.UserService;
import com.lmqb.util.NumberToCN;
import com.lmqb.util.StrUtil;


@Controller
public class LoanAgreementController extends BaseController{

    @Autowired
	private UserService userService;
    
    @Autowired
    private RepayService repayService;
    
    @Autowired
    private CfgParamsService cfgParamsService;
	
	@SuppressWarnings("deprecation")
	@RequestMapping("loanAgreement")
	public String loanAgreement(HttpSession session,String repayNo,String userId,String loanNo,String bankAccount,String bankName,HttpServletRequest request) throws ParseException, UnsupportedEncodingException{
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		DateFormat df0 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		DecimalFormat df = new DecimalFormat("######0.00");   
		String bankName1 = new String(bankName.getBytes("iso8859-1"),"utf-8");
		UBaseInfo baseInfo=userService.selectBaseInfo(Integer.parseInt(userId));
		SysConfig pmkey = cfgParamsService.selectBypmkey();
		String pmValue = pmkey.getPmValue();//配置信息利率
		LmRepay lmrepay = repayService.selectByPrimaryKey1(repayNo);
		LmLoan loan = repayService.selectByPrimaryKey(loanNo);
		Date intToTime = StrUtil.intToTime(lmrepay.getRepayEndTime());
		Date intToTime2 = StrUtil.intToTime(loan.getLoanTime());
		String format = dateFormat.format(intToTime2);
		String format1= dateFormat.format(intToTime);
		Short days = loan.getLoanDays();
		request.setAttribute("Name", baseInfo.getUserName());
		request.setAttribute("IDcard",baseInfo.getCardNum() );
		request.setAttribute("loanTime",format);
		request.setAttribute("repayEndTime",format1);
		request.setAttribute("Days", days);
		request.setAttribute("bankAccount", bankAccount);
		request.setAttribute("bankName", bankName1);
		request.setAttribute("loanMoney",loan.getLoanMoney());
		String montrayUnit = NumberToCN.number2CNMontrayUnit(loan.getLoanMoney());
		BigDecimal temp = new BigDecimal(365);
		request.setAttribute("loanRate", loan.getLoanRate().multiply(temp));
		request.setAttribute("userId", Integer.parseInt(userId));
		request.setAttribute("montrayUnit", montrayUnit);
		double rateMoney = loan.getLoanMoney().setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
		double rate = loan.getLoanRate().doubleValue();
		request.setAttribute("rateMoney",df.format(rateMoney*12*days/100/365 ));
		BigDecimal temp1 = new BigDecimal(rateMoney*12*days/100/365);
		request.setAttribute("rateD",NumberToCN.number2CNMontrayUnit(temp1));
		double fuwuFee=rateMoney*(365-12)*days*(Integer.parseInt(pmValue))/100/365;
		double fuwuFeeM=rateMoney*(365-12)*(Integer.parseInt(pmValue))/100/365;
		request.setAttribute("fuwuRate", df.format(fuwuFee));//服务费
		request.setAttribute("fuwufeeM", df.format(fuwuFeeM));
		BigDecimal temp2 = new BigDecimal(fuwuFee);
		System.out.println(df.format(fuwuFee)+"------"+NumberToCN.number2CNMontrayUnit(temp2));
		request.setAttribute("fuwuRateD", NumberToCN.number2CNMontrayUnit(temp2));
		MenuTitle menuTitle=new MenuTitle();
		menuTitle.setParent("用户管理");
		menuTitle.setMenu("借款协议");
		session.setAttribute(menuTitleF, menuTitle);
		return "loanAgreement";
	}
}
